<!--
 * @Description: 构建表单的基础方法++提交表单的数据处理
 * @ComposerRequire: 
 * @ComposerRemove: 
 * @Author: 阿海 <764882431@qq.com>
 * @Date: 2021-05-18 11:37:42
 * @LastEditTime: 2021-07-09 15:11:20
 * @LastEditors: 阿海
-->

<script>
    //构建表单
    function builderForm(resData){
        layui.use(['form','element','upload'],function(){
            var html = '',form = layui.form,$ = layui.$,upload=layui.upload;
            //构建所有的表单项
            html += builderFormItems(resData);
            // console.log(resData);
            // console.log(builderFormItems(resData));
            $("#myForm").html(html);
            form.render();
        })
        //表单提交数据
        var unsetFields = ["upfile","file"]; //过滤字段
        layui.use(["form","layer"], function(){
            var form = layui.form,layer=layui.layer;
            //自定义验证条件
            builderFormVerify(form);
            form.on("submit(formUrl)", function(obj){
             
                $(obj.elem).addClass("layui-btn-disabled");
                $(obj.elem).prepend("<i class='layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop'></i>")
                $(obj.elem).attr("disabled","disabled");
                for(var i=0; i<unsetFields.length; i++){
                    if(obj.field[unsetFields[i]] != undefined){delete obj.field[unsetFields[i]];} 
                } 

               
              
                //触发dipatch类的过渡页面
                var http = new Request({token_url:api.token_url,async:false,token_store_name:'post_form'})
                var hash = location.hash.slice(1) == '' ? 'homepage' : location.hash.slice(1);
                var res =  (resData.post_url).indexOf('?')>0?http.request("post",resData.post_url+"&hash="+hash,obj.field):http.request("post",resData.post_url+"?hash="+hash,obj.field); 
                   
                    if(res.code == 0){
                        builderDispatch(res.data);
                    }else{
                        layer.msg(res.msg,function(){});
                        $(obj.elem).removeClass("layui-btn-disabled");
                        $(obj.elem).removeAttr("disabled");
                        $(obj.elem).children("i").remove();
                    }
                
                return false;
            });
        });
    }

   
</script>



{include file='builder/form/form_items'}
{include file='builder/form/form_verify'}
